package com.example.administrator.giftspeaking;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import base.BaseActivity;
import fragment.CategoryFragment;
import fragment.HomeFragment;
import fragment.HotFragment;
import fragment.ProfileFragment;

public class MainActivity extends BaseActivity {
    //fragment的管理器
    private FragmentManager mManager;
    private RadioGroup mRadioGroup;
    //当前是否被选中的fragment
    private Fragment mCurrentFragment;
    private RadioButton rb_main_home;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        setListener();
        //////////

    }
    @Override
    public void setListener() {
        super.setListener();
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                Class <? extends  Fragment> clazz = null;
                switch (checkedId) {
                    case R.id.rb_main_hot :
                        clazz = HotFragment.class;
                        break;
                    case R.id.rb_main_home :
                        clazz = HomeFragment.class;
                        break;
                    case R.id.rb_main_category :
                        clazz = CategoryFragment.class;
                        break;
                    case R.id.rb_main_profile :
                        clazz = ProfileFragment.class;
                        break;
                }
                showAndhide(R.id.fragment_main,clazz);
            }
        });
    }

    @Override
    public void initView() {
        super.initView();
        final ActionBar supportActionbar = getSupportActionBar();
        if(supportActionbar!= null) {
            supportActionbar.hide();

        }
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_main);
        rb_main_home = (RadioButton)findViewById(R.id.rb_main_home);

    }

    @Override
    protected void initData() {
        super.initData();
        mManager = getSupportFragmentManager();
        rb_main_home.setChecked(true);
        showAndhide(R.id.fragment_main, HomeFragment.class);
    }

    private void showAndhide(int contentId, Class<? extends Fragment> clazz){
        //判断当前的fragment和需要替换的fragment是不是同一个
        if(mCurrentFragment!=null&& mCurrentFragment.getClass().getSimpleName().equals(clazz.getClass().getSimpleName())) {
            return;
        }

        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment fragment = null;

        try {
            //判断fragment有没有添加过
            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
            if(fragmentByTag!=null) {
                //显示需要显示的fragment
                transaction.show(fragmentByTag);
                //隐藏当前的fragment
                transaction.hide(mCurrentFragment);
                //记录当前显示的fragment
                mCurrentFragment = fragmentByTag;
            }else {
                //通过无参的 公开的构造函数来创建fragment实例
                fragment = clazz.newInstance();
                //当前的fragment有没有添加过
                //把fragment添加到fragmentmanager里面
                transaction.add(contentId,fragment,clazz.getSimpleName());
                //隐藏当前的显示的fragment
                if(mCurrentFragment!= null) {
                    transaction.hide(mCurrentFragment);
                }
                mCurrentFragment = fragment;
            }
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();

        }


    }
}
